package com.ccc.boot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ccc.boot.constant.CommonConstant;
import com.ccc.boot.pojo.Query;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.SysDictInfo;
import com.ccc.boot.system.service.SysDictInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 〈数据字典从表——REST接口〉
 *
 * @author superC
 * @date 2022/3/30
 * @version 0.1.0
 */
@RestController
@RequestMapping(CommonConstant.SYS_URI_PREFIX + "/dictInfo")
public class SysDictInfoController {

    protected static final Logger logger = LoggerFactory.getLogger(SysDictInfoController.class);

    @Resource
    private SysDictInfoService sysDictInfoService;

    /**
     * 新增
     */
    @PostMapping()
    public ResponseResult add(@RequestBody SysDictInfo sysDictInfo) {
        // 业务逻辑
        boolean created = sysDictInfoService.add(sysDictInfo);
        if (created) {
            return ResponseResult.success("新增成功");
        }

        return ResponseResult.failure(ResponseCode.DATA_ADD_FAILURE);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public ResponseResult delete(@RequestBody SysDictInfo sysDictInfo) {
        // 业务逻辑
        boolean deleted = sysDictInfoService.delete(sysDictInfo);
        if (deleted) {
            return ResponseResult.success(ResponseCode.DATA_DELETE_SUCCESS);
        }

        return ResponseResult.failure(ResponseCode.DATA_DELETE_FAILURE);
    }

    /**
     * 修改
     */
    @PutMapping()
    public ResponseResult update(@RequestBody SysDictInfo sysDictInfo) {
        // 业务逻辑
        boolean updated = sysDictInfoService.update(sysDictInfo);
        if (updated) {
            return ResponseResult.success("修改成功");
        }

        return ResponseResult.failure(ResponseCode.DATA_UPDATE_FAILURE);
    }

    /**
     * 分页
     */
    @GetMapping("/pageList")
    public ResponseResult pageList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        IPage<SysDictInfo> iPage = sysDictInfoService.page(query);
        return ResponseResult.success("获取数据成功", iPage);
    }

    /**
     * 查询
     */
    @PostMapping("/getList")
    public ResponseResult getList(@RequestBody SysDictInfo sysDictInfo){
        List<SysDictInfo> list = sysDictInfoService.getList(sysDictInfo);
        return ResponseResult.success("查询成功",list);
    }

}
